package cn.bdqn.service.wx.impl;

import cn.bdqn.dao.wx.EntryDao;
import cn.bdqn.dao.wx.SwiperDao;
import cn.bdqn.dao.wx.UserDao;
import cn.bdqn.entity.DecryptInfo;
import cn.bdqn.entity.Entry;
import cn.bdqn.entity.SwiperImage;
import cn.bdqn.entity.UserInfo;
import cn.bdqn.service.wx.IndexService;

import cn.bdqn.utils.WXCliceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ProjectName: ycbdqn
 * @Package: cn.bdqn.service.wx.impl
 * @Author: huat
 * @Date: 2020/2/29 22:42
 * @Version: 1.0
 */
@Service
public class IndexServiceImpl implements IndexService {
    @Value("${wx.appid}")
    private String appid;//通过配置文件获取小程序的appid
    @Value("${wx.secret}")
    private String secret;//通过配置文件获取小程序的secret
    @Autowired
    SwiperDao swiperDao;
    @Autowired
    UserDao userDao;
    @Autowired
    EntryDao entryDao;
    /**
     * 获取轮播图
     * @return 轮播图集合
     */
    @Override
    public List<SwiperImage> getSwiperImage() {
        return swiperDao.getSwiperImage();
    }

    /**
     * 获取用户信息并保存
     */
    @Transactional
    @Override
    public int addUser(DecryptInfo decryptInfo) {
            int result=0;
           UserInfo user= WXCliceUtils.getSessionKey(appid,secret,decryptInfo);
          if(null!=user){
              //查询用户是否之前登陆过
              Integer userResult=userDao.getUserByOpenId(user.getOpenId());

              if(null!=userResult){
                  user.setId(userResult);
                  int loginCecond=userDao.getUserCecondById(userResult);
                  loginCecond=loginCecond+1;
                  user.setLoginCecond(loginCecond);
                  result= userDao.updateUserById(user);
              }else {

                  user.setLoginCecond(1);
                  result= userDao.addUser(user);
              }
          }

        return result;
    }
    /**
     * 获取就业学员信息
     */
    @Override
    public List<Entry> getEntry() {
        List<Entry> entryList =entryDao.getEntry();
        for (Entry entry:entryList) {
            entry.setEntryPhoneNo(entry.getEntryPhoneNo().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
        }
        return entryList;
    }
    /**
     * 解密并保存用户手机号
     * decryptInfo 加密信息
     */
    @Transactional
    @Override
    public int addPhone(DecryptInfo decryptInfo) {
        int result=0;
        UserInfo user= WXCliceUtils.getSessionKey(appid,secret,decryptInfo);
        if(null!=user){
            //查询用户是否之前登陆过
            Integer userResult=userDao.getUserByOpenId(user.getOpenId());

            if(null!=userResult){
                user.setId(userResult);

                result=  userDao.updateUserPhone(user);
            }else {

                result= userDao.addUser(user);
            }
        }
        return result;
    }
    /**
     * 根据id查询轮播图详情
     * @param id
     * @return
     */
    @Override
    public String getSwiperById(int id) {
        return swiperDao.getSwiperById(id);
    }
}
